package com.source.baseData.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.source.barcodeMachineGrantAndRecovery.entity.HouseMachine;
import com.source.barcodeMachineGrantAndRecovery.mapper.HouseMachineMapper;
import com.source.baseData.entity.BarcodeMachine;
import com.source.baseData.mapper.BarcodeMachineMapper;
import com.source.baseData.service.BarcodeMachineService;
import com.source.baseData.vo.BarcodeMachineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author fengq
 */
@Service
public class BarcodeMachineServiceImpl extends ServiceImpl<BarcodeMachineMapper, BarcodeMachine> implements BarcodeMachineService {

    @Autowired
    BarcodeMachineMapper barcodeMachineMapper;

    @Autowired
    HouseMachineMapper houseMachineMapper;


    @Override
    public IPage<BarcodeMachineVo> getPage(Page<BarcodeMachineVo> page) {
        return barcodeMachineMapper.getPageVo(page);
    }

    @Override
    public BarcodeMachineVo getOneVoById(String barcodeMachineId) {
        return barcodeMachineMapper.getOneVoById(barcodeMachineId);
    }

    @Override
    public List<BarcodeMachine> findByHouseId(String slaughterHouseId) {
        QueryWrapper<HouseMachine> houseWrapper = new QueryWrapper<>();
        houseWrapper.select("machineNumber").eq("houseId", slaughterHouseId);
        //根据屠宰场id在屠宰市场脚标机内查询到相关数据，取出脚标机number
        List<HouseMachine> list = houseMachineMapper.selectList(houseWrapper);
        List<String> machines = new ArrayList<>();

        for (HouseMachine houseMachine : list) {
            machines.add(houseMachine.getMachineNumber());
        }

        QueryWrapper<BarcodeMachine> wrapper = new QueryWrapper<>();
        wrapper.in("barcodeMachineNumber", machines);
        return list(wrapper);
    }

    @Override
    public List<BarcodeMachine> getPageByLike(String keyWord) {
        return barcodeMachineMapper.getPageByLike("%"+keyWord+"%");
    }

    @Override
    public BarcodeMachine findByMachineNumber(String machineNumber) {
        return barcodeMachineMapper.findByMachineNumber(machineNumber);
    }

    @Override
    public List<BarcodeMachine> findUnGranted() {
        return barcodeMachineMapper.findUnGranted();
    }
}
